package com.fourth.shopping.controller;


import com.fourth.shopping.commons.TableData;
import com.fourth.shopping.entity.Item;
import com.fourth.shopping.service.ItemService;
import com.fourth.shopping.vo.PageCondition;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("items")
public class ItemController {
    @Autowired
    private ItemService itemService;

    @RequestMapping("getAllItems.do")
    public List<Item> getAllItems(){
        return itemService.getAllItems();
    }

    @RequestMapping("getItemsAsPage.do")
    public TableData<Item> getItemsAsPage(PageCondition condition){
        return itemService.getItemsAsPage(condition);
    }

    @RequestMapping("getItemById.do")
    public Item getItemById(@RequestParam("id") String id){
        return itemService.getItemById(id);
    }

    @PostMapping("addNewItem.do")
    public Boolean addNewItem(Item item){
        return itemService.addNewItem(item);
    }

    @PostMapping("deleteItem.do")
    public Boolean deleteItem(@RequestParam("id") Integer id){
        return itemService.deleteItem(id);
    }

    @PostMapping("modifyItem.do")
    public Boolean modifyItem(Item item){
        if(item == null || item.getId() == null){
            return false;
        }
        itemService.modifyItem(item);
        return false;
    }
}
